package com.ww.learn.generic

// Array<out Fruit>用于传递Fruit参数类型的协变
// 要使用协变，需要向Kotlin编译器保证，不会调用任何方法，来对Array<T>发送具有给定参数类型的任何值。
fun copyFromTo(from: Array<out Fruit>, to: Array<Fruit>) {
    for (i in 0 until from.size) {
        to[i] = from[i]
    }
}

fun main() {
    val fruitsBasket1 = Array<Fruit>(3) { _ -> Fruit() }
    val fruitsBasket2 = Array<Fruit>(3) { _ -> Fruit() }
    copyFromTo(fruitsBasket1, fruitsBasket2)

    val bananaBasket = Array<Banana>(3) { _ -> Banana() }
    copyFromTo(bananaBasket, fruitsBasket2)
}